﻿import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Screen;
import mx.containers.Panel;

import mx.events.ItemClickEvent;
import mx.events.FlexNativeWindowBoundsEvent;
import mx.managers.PopUpManager;
import mx.containers.TitleWindow;

import friendfeed.api.FriendFeed;
import friendfeed.api.data.Entry;
import friendfeed.api.data.Profile;
import friendfeed.api.events.FriendFeedEvent;

public var preferences:SharedObject = SharedObject.getLocal("preferences");
public var panels:Array = [];

private var connected:Boolean = false;
private var login:LoginWindow;
private var friendFeed:FriendFeed;
private var apikey:String = "";

private function creationCompleteHandler(e:Event):void
{
	friendFeed = new FriendFeed();
	dock();
	if (preferences.data.username && preferences.data.remotekey)
		connect();
	
	loadPublicFeed(publicFeedPanel);
	methodsViewStack.selectedChild = publicFeedPanel;
	methodsTab.selectedIndex = 1;
}

private function connect():void
{
	friendFeed = new FriendFeed(preferences.data.username, preferences.data.remotekey);
	friendFeed.addEventListener(FriendFeedEvent.ON_VALIDATE_RESULT, onValidateHandler);
	friendFeed.addEventListener(FriendFeedEvent.ON_ERROR, onErrorHandler);
	friendFeed.validate(preferences.data.username, preferences.data.remotekey);
	setStatus("Validating user on " + new Date());
	
}

private function onAuthorizeHandler(e:Event):void
{
	preferences.data.username = login.username;
	preferences.data.remotekey = login.remotekey;
	connect();
}

private function onValidateHandler(e:FriendFeedEvent):void
{
	setStatus("Connection successful");	
	lblSignin.text = preferences.data.username;
	btnSignin.label = "Logout";
	connected = true;
}

private function onErrorHandler(e:FriendFeedEvent):void
{
	setStatus("Error" + e.data);
}

private function onItemClick(event:ItemClickEvent):void
{
	methodsViewStack.selectedChild = this[event.item.data +'Panel'];
	var panel:Panel = (methodsViewStack.selectedChild as Panel);
	switch(event.item.data)
	{
		case "publicFeed": loadPublicFeed(panel); break;
		case "userFeed": userPublicFeed(panel); break;
	}
}

private function loadPublicFeed(panel:Panel):void
{	
	friendFeed.addEventListener(FriendFeedEvent.ON_PUBLIC_FEED_RESULT, onPublicFeedHandler);
	friendFeed.loadPublicFeed();
	this.panels[FriendFeedEvent.ON_PUBLIC_FEED_RESULT] = panel;
	setStatus("Loading public feed...");
}

private function userPublicFeed(panel:Panel):void
{
	if (!preferences.data.username)
		return;

	friendFeed.addEventListener(FriendFeedEvent.ON_USER_FEED_RESULT, onUserFeedHandler);
	friendFeed.loadUserFeed(preferences.data.username);
	this.panels[FriendFeedEvent.ON_USER_FEED_RESULT] = panel;
	setStatus("Loading user feed...");
}

private function onPublicFeedHandler(event:FriendFeedEvent):void
{
	setStatus("Public feed loaded on " + new Date());
	var panel:Panel = (this.panels[event.type] as Panel);
	addEntries(event.data, panel);
}

private function onUserFeedHandler(event:FriendFeedEvent):void
{
	setStatus("User feed loaded on " + new Date());
	var panel:Panel = (this.panels[event.type] as Panel);
	addEntries(event.data, panel);
}

private function addEntries(entry:*, panel:Panel):void
{
	if (entry is Entry)
		addEntry(entry, panel);
	else if (entry is Array)
	{
		var length:uint = entry.length;
		for (var i:uint = 0; i < length; i++)
			addEntry(entry[i], panel);
	}
}

private function addEntry(entry:Entry, panel:Panel):void
{
	var ev:EntryView = new EntryView();
	panel.addChild(ev);
	ev.entry = entry;
}

private function btnSigninClick(MouseEvent:Event):void
{
	if (!connected)
	{
		login = LoginWindow(PopUpManager.createPopUp(this, LoginWindow, true));
		PopUpManager.centerPopUp(login);

		login.username = preferences.data.username;
		login.remotekey = preferences.data.remotekey;
		login.addEventListener(LoginWindow.ON_AUTHORIZE, onAuthorizeHandler);
		setStatus("Remote login");
	} 
	else
	{
		connected = false;
		preferences.data.username = "";
		preferences.data.remotekey = "";
		setStatus("Logout");
		
		lblSignin.text = "You're not logged in";
		btnSignin.label = "Sign in";
	}
}

public function dock():void
{
	var screen:Screen = getCurrentScreen();
	this.nativeWindow.x = screen.visibleBounds.width - this.nativeWindow.width;
	this.nativeWindow.y = screen.visibleBounds.top;
	this.nativeWindow.height = screen.visibleBounds.height;
}
        
private function getCurrentScreen():Screen
{
	return Screen.getScreensForRectangle(this.nativeWindow.bounds)[0];
}

public function setStatus(str:String):void
{
	this.status = str;
}